<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Logic;


use App\Common\Tool;
use App\Exceptions\ApiException;
use Illuminate\Support\Facades\DB;

class RegisterLogic extends Logic
{
    protected $param;

    /**
     * 处理注册
     * @param $param
     *        is_check_phone 1校验手机,其它不校验
     *        phone_num 可选
     *        nick_name
     *        password
     *        ua
     *        type 1手机登录2微信登录
     *        wx_id 可选参数
     *        sex
     * @return array
     * @throws ApiException
     */
    public function handle($param)
    {
        $this->param = $param;
        if ($this->param['is_check_phone'] == 1 && $this->checkPhone()) {
            throw new ApiException('手机号账户已经存在', 8);
        }
        $user_data = $this->createUserData();
        if (($id = DB::table('t_player_info')->insertGetId($user_data)) == 0) {
            throw new ApiException('注册失败');
        }
        LoginLogic::instance()->insertLoginRec($id, $this->param['type']);
        //发送附件邮件
        $this->email($id);
        //组合返回的内容
        $user_data['vip_level']        = 0;
        $user_data['vip_name']         = '普通会员';
        $user_data['is_first_time']    = 1;
        $user_data['paiwei_rank_name'] = '敬请期待';
        $user_data['paiwei_rank']      = 0;
        $user_data['id']               = $id;
        $user_data['type']             = $param['type'];
        $user_data['room_ticket']      = 0;
        $user_data['win_board']        = 0;
        $user_data['fail_board']       = 0;
        $user_data['peace_board']      = 0;
        $user_data['is_binding'] = 0;
        return LoginLogic::instance()->getReturnData($user_data);
    }


    private function createUserData()
    {
        $now                       = date('Y-m-d H:i:s', time());
        $user_info                 = array();
        $user_info["nick_name"]    = $this->param['nick_name'];
        $user_info["passwd_md5"]   = md5($this->param['password']);
        $user_info["passwd"]       = $this->param['password'];
        $user_info["status"]       = 1;        // 0：为封号的, 1:表示账号可用
        $user_info["sex"]          = $this->param['sex'] ?? 0;
        $user_info["ua"]           = $this->param['ua'];
        $user_info["diamonds"]     = 88;
        $user_info["gold"]         = 15000;
        $user_info["create_time"]  = $now;
        $user_info["update_time"]  = $now;
        $user_info["skey"]         = Tool::skey();
        $user_info["login_ip"]     = $_SERVER["REMOTE_ADDR"];
        $user_info["paiwei_score"] = 0;
        $user_info["avater_url"]   = $this->param['avater_url'] ?? '';
        if (!empty($this->param['wx_id'])) {
            $user_info['wx_id'] = $this->param['wx_id'];
        }
        if (!empty($this->param['phone_num'])) {
            $user_info["phone_num"] = $this->param['phone_num'];
        }
        return $user_info;
    }

    private function checkPhone()
    {
        if (DB::table('t_player_info')->where('phone_num', $this->param['phone_num'])->value('phone_num')) {
            return true;
        }
        return false;
    }

    private function email($uid)
    {
        if ($object = ActivityLogic::instance()->activityConfInfo(1)) {
            DB::table('t_mail_info')->insert([
                'uid' => $uid,
                'type' => 2,
                'head' => '首测玩家专享礼',
                'content' => '感谢您参与“贵州英雄鸡”首次限量删档测试，我们为首测玩家准备了丰厚的礼品！您可以体验紧张刺激的快抢赛，谁与争锋的排位赛，悠闲欢快的金币赛，或是好友相约的房间模式！更多玩法敬请关注微信公众号——贵州英雄鸡！',
                'rule' => $object->rule,
                'status' => 1
            ]);
        }
    }

}